//
//  EnsureListViewController.m
//  Mahjong
//
//  Created by Chen on 16/9/18.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "EnsureListViewController.h"
#import "EnsureGoodsTableViewCell.h"
#import "EnsureListInfoTableViewCell.h"
#import "SuccessViewController.h"
#import "PayWayViewController.h"
#import "AddressViewController.h"

static NSString *identifierForEnsureGoodsTableViewCell = @"EnsureGoodsTableViewCell";
static NSString *identifierForEnsureListInfoTableViewCell = @"EnsureListInfoTableViewCell";

@interface EnsureListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UITableView *tableView;

@property (nonatomic , strong) UIView *buttomView;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *tel;
@property (nonatomic, copy) NSString *address;


@end

@implementation EnsureListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"确认订单";
    self.navigationController.navigationBar.translucent = NO;
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"MRName"] != nil) {
        self.name = [[NSUserDefaults standardUserDefaults] objectForKey:@"MRName"];
        self.tel = [[NSUserDefaults standardUserDefaults] objectForKey:@"MRTel"];
        self.address = [[NSUserDefaults standardUserDefaults] objectForKey:@"MRAddress"];
    }
    [self createTableView];
    
    [self createButtomView];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - kTabbarHeight- kNavigationBarHeight) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:identifierForEnsureGoodsTableViewCell bundle:nil] forCellReuseIdentifier:identifierForEnsureGoodsTableViewCell];
    [self.tableView registerNib:[UINib nibWithNibName:identifierForEnsureListInfoTableViewCell bundle:nil] forCellReuseIdentifier:identifierForEnsureListInfoTableViewCell];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return 2;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        EnsureListInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForEnsureListInfoTableViewCell forIndexPath:indexPath];
        cell.nameLabel.text = self.name;
        cell.telLabel.text =self.tel;
        cell.addressLabel.text = self.address;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 1){
        EnsureGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForEnsureGoodsTableViewCell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        
        if (indexPath.section == 2) {
            cell.textLabel.text = @"配送方式";
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(kUIScreenWidth - 120, 0, 100, 60)];
            label.text = @"快递￥20";
            cell.accessoryView = label;
        } else if(indexPath.section == 3){
            cell.textLabel.text = @"付款方式";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(kUIScreenWidth - 160, 0, 100, 60)];
            label.text = @"支付宝";
            [cell addSubview:label];
        } else {
            cell.textLabel.text = @"买家留言";
        }
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 85;
    } else if (indexPath.section == 1){
        return 110;
    } else {
        return 60;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        AddressViewController *vc = [[AddressViewController alloc] init];
        [vc blockstring:^(NSInteger str, NSString *address, NSString *name, NSString *tel) {
            self.name = name;
            self.address = address;
            self.tel = tel;
            [self.tableView reloadData];
            
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.section == 3) {
        PayWayViewController *payWayVC = [[PayWayViewController alloc] init];
        [self.navigationController pushViewController:payWayVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 4) {
        return 20;
    }
    return 0.01;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //设置separatorInset(iOS7之后)
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //设置separatorInset(iOS7之后)
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)createButtomView{
    self.buttomView = [[UIView alloc] initWithFrame:CGRectMake(0, kUIScreenHeight - kTabbarHeight - kNavigationBarHeight, kUIScreenWidth, kTabbarHeight)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 150, kTabbarHeight - 10)];
    label.text = @"合计：￥ 1592";
    label.textColor = [UIColor redColor];
    [self.buttomView addSubview:label];
    
    UIButton *buyBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    buyBtn.frame = CGRectMake(kUIScreenWidth - 100, 7.5, 80, kTabbarHeight - 15);
    [buyBtn setTitle:@"提交订单" forState:(UIControlStateNormal)];
    [buyBtn setBackgroundColor:[UIColor redColor]];
    
    buyBtn.layer.cornerRadius = 5;
    buyBtn.clipsToBounds = YES;
    [self.buttomView addSubview:buyBtn];
    [buyBtn addTarget:self action:@selector(buyBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 1)];
    lineView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    [self.buttomView addSubview:lineView];
    
    [self.view addSubview:self.buttomView];
}

- (void)buyBtnAction:(UIButton *)btn{
    SuccessViewController *vc = [[SuccessViewController alloc] init];
    [self.navigationController pushViewController:vc  animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
